//changeHeight(670);
$(function() {
//by WuK
	$("#datagrid").height(parent.$("#mainPanle").height()-14);
	
	/**
	  * 获取指定URL的参数值
	  * @param url	指定的URL地址
	  * @param name	参数名称
	  * @return	参数值
	  */
	 function getUrlParam(url,name){
	     var pattern = new RegExp("[?&]"+name+"\=([^&]+)", "g");
	     var matcher = pattern.exec(url);
	     var items = null;
	     if(null != matcher){
	             try{
						items = decodeURIComponent(decodeURIComponent(matcher[1]));
	             }catch(e){
	                     try{
	                             items = decodeURIComponent(matcher[1]);
	                     }catch(e){
	                             items = matcher[1];
	                     }
	             }
	     }
	     return items;
	}
	 
	 //点击菜单时，菜单名显示在中间页面上；
	 var clickurl = decodeURIComponent(location.href);
	 var menuName = getUrlParam(clickurl,'menuName');
	 if(null != menuName) // 点击菜单进入页面
	 {
		 $("#datagrid").datagrid({
		   title:menuName   //列表标题
	     })
	 }
	
	//当前使用的bol
	var currentBol;
	//全局id
	var globalId="";
	var cmenu;
	function createColumnMenu() {
		cmenu = $('<div/>').appendTo('body');
		cmenu.menu({
			onClick : function(item) {
				if (item.iconCls == 'icon-ok') {
					$('#datagrid').datagrid('hideColumn', item.name);
					cmenu.menu('setIcon', {
						target : item.target,
						iconCls : 'icon_empty'
					});
				} else {
					$('#datagrid').datagrid('showColumn', item.name);
					cmenu.menu('setIcon', {
						target : item.target,
						iconCls : 'icon-ok'
					});
				}
			}
		
		});
		
		var fields = $('#datagrid').datagrid('getColumnFields');
		for ( var i = 0; i < fields.length; i++) {
			var field = fields[i];
			var col = $('#datagrid').datagrid('getColumnOption', field);
			if (col.hidden == true) {
				cmenu.menu('appendItem', {
					text : col.title,
					name : field,
					iconCls : 'icon-empty'
				});
			} else {
				cmenu.menu('appendItem', {
					text : col.title,
					name : field,
					iconCls : 'icon-ok'
				});
			}
		}
	}
	$("#search").click(function() {
		var queryParams = $('#datagrid').datagrid('options').queryParams;
		queryParams.search_bolName = $("#search_bol").combo("getText").trim();
		queryParams.search_domain = $("#search_domain").combo("getValue").trim();
		currentBol= $("#search_bol").combo("getText").trim();
		$("#datagrid").datagrid('reload');
	});
	
	comboBox('search_region_id', 'comboBoxAction.do?action=getRegionId');
	
	comboBox('search_domain','comboBoxAction.do?action=getAnyDomain',function(){
		var data = $('#search_domain').combobox('getData');
   	 	$("#search_domain").combobox('select',data[0].ID);	
	});
	
	$("#search_domain").combobox({
		'onChange' : function(n, o) {
			//bol下拉框
	    	var url = 'comboBoxAction.do?action=getBolByDomain&search.domain_type=' + n.trim();
			$('#search_bol').combobox({
				url:url,
				valueField:'ID',
				textField:'NAME',
				loadFilter: function(data){
	    			data.list.unshift({ "ID": "", "NAME": "全部" });
	    			return data.list;
				}
			});
		}
	});
	
	comboBox('edit_BOL_ID','comboBoxAction.do?action=getBolNameAndId',function(){
		  var data = $('#edit_BOL_ID').combobox('getData');
		  $("#edit_BOL_ID").combobox('select',data[0].ID);
		 });
	comboBox('edit_program_id','comboBoxAction.do?action=getOnceProgram',function(){
		  var data = $('#edit_program_id').combobox('getData');
		  $("#edit_program_id").combobox('select',data[0].ID);
		 });
	$('#batch_program_id').combobox({
		url:'comboBoxAction.do?action=getOnceProgram',
		valueField:'ID',
		textField:'NAME',
		loadFilter: function(data){
			data.list.unshift({ "ID": "", "NAME": "暂不修改" });
			return data.list;
		}
	});
	var $win = $('#edit-window').window({
		title : '新增',
		width : 450,
		height : 480,
		top : ($(window).height() - 480) * 0.5,
		left : ($(window).width() - 450) * 0.5,
		shadow : true,
		modal : true,
		closed : true,
		minimizable : false,
		maximizable : false,
		collapsible : false
	});

	var $batchWin = $('#batchUpdate-window').window({
		width : 450,
		height : 480,
		top : ($(window).height() - 480) * 0.5,
		left : ($(window).width() - 450) * 0.5,
		shadow : true,
		modal : true,
		closed : true,
		minimizable : false,
		maximizable : false,
		collapsible : false
	});
	
	var form = $win.find("form");
	$('#edit_next_exec_time').datetimebox({    
		panelHeight:500
	});
	$('#batch_next_exec_time').datetimebox({    
		panelHeight:500
	});
	$("#add").click(function() {
		if($("#search_bol").combo("getText").trim()=='全部')
		{
			$.messager.alert('提示', '必须选择一个Bol才能添加定时任务', 'info');
			return;
		}
		$win.window('open');
		$('#edit_task_minute').numberbox('setValue',0);
		$('#edit_task_hour').numberbox('setValue',0);
		$('#edit_task_day').numberbox('setValue',0);
		$('#edit_task_month').numberbox('setValue',0);
		$('#edit_task_year').numberbox('setValue',0);
		$('#edit_task_week').numberbox('setValue',0);
		form.url = 'crontabAction.do?action=addTask&search.bolName='+$("#search_bol").combobox('getText');

	});

	$("#submitForm").click(function() {
		if($('#edit_task_minute').numberbox('getValue')==0 && $('#edit_task_hour').numberbox('getValue')==0 && $('#edit_task_day').numberbox('getValue')==0 && $('#edit_task_month').numberbox('getValue')==0 && $('#edit_task_year').numberbox('getValue')==0 && $('#edit_task_week').numberbox('getValue')==0)
		{
			$.messager.alert('提示', '时间间隔不能全为0', 'info');
			return;
		}
		form.form('submit', {
			url : form.url,
			success : function(data) {
				data = toJson(data);
				if (data.flag == 0) {
					$("#datagrid").datagrid('reload');
					$win.window('close');
				} else {
					$.messager.alert('错误', data.flag, 'error');
				}
			}
		});
	});

	$("#clearForm").click(function() {
		form.form("clear");
		$("#edit_task_id")
		.textbox('setValue', globalId);
		  $("#edit_Is_Valid ").combobox('setValue',1);
		  if($('#edit_program_id').combobox('getValue', "")==""){
				var data = $('#edit_program_id').combobox('getData');
				  $("#edit_program_id ").combobox('select',data[0].ID);
			}
	});

	var formb = $('#ffb');
	
	$("#submitFormb").click(function() {
		formb.form('submit', {
			url : formb.url,
			success : function(data) {
				data = toJson(data);
				if (data.flag == 0) {
					$("#datagrid").datagrid('reload');
					$batchWin.window('close');
				} else {
					$.messager.alert('错误', data.flag, 'error');
				}
			}
		});
	});
	
	$("#modify").click(function() {
		form.form("clear");
		var rows = $('#datagrid').datagrid('getSelections');
		if (rows.length == 0) {
			$.messager.alert('警告', '请选择需修改的记录!', 'warning');
			return;
		}
		if(rows.length == 1)
		{
			var task_id = rows[0].task_id;
			currentBol = rows[0].bol_name;
			$.ajax({
				url : 'crontabAction.do?action=getOneTask&search.task_id='
					+ task_id+'&'+'search.bolName='+currentBol,
				type : 'POST',
				success : function(data) {
				data = toJson(data);
				var taskInfo = data.taskInfo;
				taskInfo.next_exec_time = rows[0].next_exec_time;
				$win.window('open');
				$win.window('setTitle','修改');
				globalId=taskInfo.task_id;
				loadData(taskInfo);
				form.url = 'crontabAction.do?action=modTask&search.bolName='+currentBol+'&search.task_id='+task_id;
			},
			error : function() {
				$.messager.alert('错误', '操作失败!', 'error');
			}
			});
		}
		else
		{
			var idAndBols = '';
			for ( var i = 0; i < rows.length; i++) {
				idAndBols = idAndBols + rows[i].task_id + '!' + rows[i].bol_name + '|';
			}
			$batchWin.window('open');
			formb.url = 'crontabAction.do?action=batchModify&search.idAndBols=' + idAndBols;
		}
	});

	$("#delete").click(function() {
		var rows = $('#datagrid').datagrid('getSelections');
		if (rows.length == 0) {
			$.messager.alert('警告', '请至少选择一条记录!', 'warning');
			return;
		}
		
		$.messager.confirm("操作提示","您确定要删除这些任务吗？",function(data) {
				if (data) {
					var task_ids = '';
					for ( var i = 0; i < rows.length; i++) {
						if (i > 0) {
							task_ids += ",";
						}
						task_ids += rows[i].task_id;
					}
					$.ajax({
						url : 'crontabAction.do?action=delTask&search.task_ids='
								+ task_ids+'&'+'search.bolName='+currentBol + '&search.domain=' + $("#search_domain").combo("getValue").trim(),
						type : 'POST',
						success : function(data) {
							data = toJson(data);
							if (data.flag == 0) {
								$("#datagrid").datagrid('reload');
							} else {
								$.messager.alert('错误',data.flag,'error');
							}
						},
						error : function() {
							$.messager.alert('错误','操作失败!','error');
						}
					});
				}
			});
	});
	
	
	var targetwin = $('#targetbolwin').window({
		title : '选择目标BOL',
		width : 350,
		height : 160,
		top : ($(window).height() - 160) * 0.5,
		left : ($(window).width() - 300) * 0.5,
		shadow : true,
		modal : true,
		closed : true,
		minimizable : false,
		maximizable : false,
		collapsible : false
	});
	
	$("#reArrange").click(function() {
		var rows = $('#datagrid').datagrid('getSelections');
		if (rows.length == 0) {
			$.messager.alert('提示', '请至少选择一条数据!');
			return;
		}
		targetwin.window('open');
	});
	
	
	$("#arrangeConfirm").click(function() {
		var rows = $('#datagrid').datagrid('getSelections');
		var taskid = '';
		for (var i = 0; i < rows.length; i++) {
			if (i != 0) {
				taskid += ',';
			}
			taskid += rows[i].task_id;
		}
		var oldbol = $('#search_bol').combobox('getText');
		var newbol = $('#bolist').combobox('getText');
		if (oldbol == newbol) {
			$.messager.alert('提示', '目标BOL与原BOL不能一致!');
			return;
		}
		MaskUtil.mask('任务调度中...');
		targetwin.window('close');
		$.ajax({
			url : 'crontabAction.do?action=reArrangeTask',
			data : 'search.oldbol=' + oldbol + '&search.newbol=' + newbol
					+ '&search.taskids=' + taskid,
			type : 'post',
			success : function(data) {
				MaskUtil.unmask();
				var datax = toJson(data);
				if (datax.flag == 0) {
					var queryParams = $('#datagrid').datagrid('options').queryParams;
					queryParams.search_bolName = $("#search_bol")
							.combobox("getText");
					$("#datagrid").datagrid('reload');

					$.messager.alert('提示', '任务调度成功!');
				} else {
					$.messager.alert('提示', datax.flag);
				}
			}
		});
	});

	comboBox('bolist', 'comboBoxAction.do?action=getBol', function() {
		var data = $('#bolist').combobox('getData');
		$("#bolist").combobox('select', data[0].ID);
	});
	
	privilege('330703');
});
function loadData(taskInfo){
	$("#edit_program_id").combobox('setValue', taskInfo.program_id);
	if($('#edit_program_id').combobox('getValue', "")==""){
		var data = $('#edit_program_id').combobox('getData');
		  $("#edit_program_id ").combobox('select',data[0].ID);
	}
	
	  if($('#edit_program_id').combobox('getValue', "")==""){
			var data = $('#edit_program_id').combobox('getData');
			  $("#edit_program_id ").combobox('select',data[0].ID);
		}
	  
	$("#edit_task_minute").textbox('setValue',
			taskInfo.task_minute);
	$("#edit_task_hour").textbox('setValue',
			taskInfo.task_hour);
	$("#edit_task_day").textbox('setValue',
			taskInfo.task_day);
	$("#edit_task_month").textbox('setValue',
			taskInfo.task_month);
	$("#edit_task_year").textbox('setValue',
			taskInfo.task_year);
	$("#edit_task_week").textbox('setValue',
			taskInfo.task_week);
	$('#edit_BOL_ID').combobox('setValue', taskInfo.BOL_ID);
	if($('#edit_BOL_ID').combobox('getValue', "")==""){
		var data = $('#edit_BOL_ID').combobox('getData');
		  $("#edit_BOL_ID ").combobox('select',data[0].ID);
	}
	$("#edit_Is_Valid").combobox('select',taskInfo.Is_Valid);
	  if($('#edit_Is_Valid').combobox('getValue', "")==""){
	  $("#edit_Is_Valid ").combobox('setValue',1);
	  }
	$("#edit_next_exec_time").datetimebox('setValue',taskInfo.next_exec_time);
			//taskInfo.next_exec_time);
}
